#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
数据库初始化修复脚本
用于解决missing table问题
"""

import os
import sys
from database_init import DatabaseInitializer

def main():
    """主函数"""
    print("🔧 开始修复数据库...")
    
    # 检查当前数据库状态
    db_path = "enterprise_analysis.db"
    if os.path.exists(db_path):
        size = os.path.getsize(db_path)
        print(f"📊 当前数据库文件大小: {size} bytes")
    
    try:
        # 初始化数据库
        db_init = DatabaseInitializer(db_path)
        print("🚀 开始初始化数据库表...")
        db_init.init_all_tables(recreate=True)
        
        # 验证表是否创建成功
        import sqlite3
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = cursor.fetchall()
        conn.close()
        
        print("\n✅ 数据库初始化成功！")
        print(f"📋 已创建表: {len(tables)} 个")
        for table in tables:
            print(f"  - {table[0]}")
        
        return True
        
    except Exception as e:
        print(f"❌ 数据库初始化失败: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1) 